<template>
  <div class="app-container">
    <!-- <el-button type="primary" @click="handleAdd">创建会员</el-button> -->

    <el-table :data="memberList" style="width: 100%;margin-top:30px;" stripe>
      <el-table-column 
        align="center" 
        label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width: 100px; height: 100px" class="user-avatar">
        </template>
      </el-table-column>
      <el-table-column 
        prop="nickname" 
        align="center" 
        label="昵称">
      </el-table-column>
      <el-table-column 
        prop="phone" 
        align="center" 
        label="电话号码">
      </el-table-column>
      <el-table-column 
        prop="integration" 
        align="center" 
        label="积分">
      </el-table-column>
      <el-table-column 
        prop="growth" 
        align="center" 
        label="成长值">
      </el-table-column>
      <el-table-column 
        prop="memberLevel" 
        align="center" 
        label="会员等级">
      </el-table-column>
      <el-table-column 
        prop="status" 
        align="center" 
        label="状态">
      </el-table-column>
      <el-table-column 
        prop="createat" 
        align="center" 
        label="注册时间">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="handleEdit(scope)">编辑</el-button> -->
          <el-button type="text" size="small" @click="handleStatus(scope)">启用/停用</el-button>
          <!-- <el-button type="text" size="small" @click="handleDetail(scope.row.id)">详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block">
        <span class="demonstration"></span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
    </div>

    <!-- 会员详情 -->
    <el-dialog :visible.sync="dialogVisible" title="会员详情">
      <el-form :model="member" label-width="80px" label-position="left">
        <el-form-item label="菜单名称">
          <el-input v-model="member.title" placeholder="菜单名称" />
        </el-form-item>
        <el-form-item label="父级菜单">
          <el-select v-model="member.parentId" placeholder="请选择">
            <el-option
              v-for="item in parentList"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="前端名称">
          <el-input v-model="member.name" placeholder="前端名称" />
        </el-form-item>
      </el-form>
      <!-- <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit">确认</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
import { getMenus, getParents, addMenu, updateMenu, delMenu } from '@/api/permission'
import { getMembers, getMemberDetail, updateMemberStatus } from '@/api/member'


const defaultRole = {
  key: '',
  name: '',
  description: '',
  routes: []
}

export default {
  data() {
    return {
      member: {},
      memberList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getMembers()
    // this.getParentList()
  },
  methods: {
    async getMembers() {
      const res = await getMembers()
      console.log("getMembers",res)
      // this.serviceRoutes = res.data
      const { pageNum, total, list} = res.data
      this.memberList = list
      this.page.currentPage = pageNum
      this.page.total = total
    },
    handleStatus(scope) {
      const member = scope.row
      const status = member.status == '启用' ? '禁用' : '启用'
      this.$confirm('确认'+status+member.nickname+'?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if(status === '启用'){
            await updateMemberStatus(member.id,1)
          }else{
            await updateMemberStatus(member.id,0)
          }
          this.$message({
            type: 'success',
            message: status+member.nickname+'成功'
          })
          this.getMembers()
        })
        .catch(err => { console.error(err) })
    },
    async handleDetail(id) {
      const res = await getMemberDetail(id);
      console.log("handleDetail",res)
      this.member = res.data
      this.dialogVisible = true
    },
    // reference: src/view/layout/components/Sidebar/SidebarItem.vue
    onlyOneShowingChild(children = [], parent) {
      let onlyOneChild = null
      const showingChildren = children.filter(item => !item.hidden)

      // When there is only one child route, the child route is displayed by default
      if (showingChildren.length === 1) {
        onlyOneChild = showingChildren[0]
        onlyOneChild.path = path.resolve(parent.path, onlyOneChild.path)
        return onlyOneChild
      }

      // Show parent if there are no child route to display
      if (showingChildren.length === 0) {
        onlyOneChild = { ... parent, path: '', noShowingChildren: true }
        return onlyOneChild
      }

      return false
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
